# -*- coding: utf-8 -*-
# @Time: 2021/12/19 14:00
# @Author: 胡志远
# @Software: PyCharm


from PyQt5.QtCore import QThread, QMutex, pyqtSignal
from file.FileContext import FileContext

import requests

fc = FileContext()


class modelController(QThread):
    typeOfChilli, num = int, int
    isFinish_signal = pyqtSignal(object)  # 1成功0失败

    def __init__(self):
        super(QThread, self).__init__()
        self._qmut = QMutex()  # 创建线程锁

    def run(self) -> None:
        """
        :return: str
        """
        self._qmut.lock()  # 加锁
        self.doSearch()
        self._qmut.unlock()  # 解锁

    def doSearch(self):
        """
        根据参数查找
        :return: 失败None，成功返回{"pic":url,"dic":属性键值对}
        """
        df = fc.get_chili() if self.typeOfChilli == 1 else fc.get_pepper()
        result = df[df.iloc[:, 0] == self.num].iloc[:, 1:]
        if not result.empty:
            dic = result.to_dict('records')[0]
            pic = "./file/pic/{}-sample{}.jpg".format("chili" if self.typeOfChilli == 1 else "pepper", self.num)
            self.isFinish_signal.emit({"pic": pic, "dic": dic})
        else:
            self.isFinish_signal.emit(None)
